package edu.chl.da.ejb.ctrl;

import edu.chl.da.ejb.core.Airport;
import edu.chl.da.ejb.core.AirportPK;
import edu.chl.da.ejb.core.Destination;
import edu.chl.da.ejb.persistency.IAirportFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Max
 */
@Stateless
public class ManageAirport implements IManageAirport {

    @EJB
    IAirportFacade iaf;

    @Override
    public Airport createAirport(String name, Destination destination) {
        Airport airport = new Airport();
        airport.setDestination1(destination);
        AirportPK airportPK = new AirportPK();
        airportPK.setDestination(destination.getName());
        airportPK.setName(name);
        airport.setAirportPK(airportPK);
        iaf.create(airport);
        return airport;
    }

    @Override
    public Airport editAirport(Airport airport) {
        if(airport == null){
            throw new NullPointerException("Airport is null");
        }
        iaf.edit(airport);
        return airport;
    }

    @Override
    public boolean removeAirport(Airport airport) {
        try {
            iaf.remove(airport);
        }
        catch(Exception e){
            return false;
        }
        return true;
    }
    @Override
    public boolean checkAirportNameAvailability(String name, Destination destination) {
        AirportPK airportPK = new AirportPK();
        airportPK.setDestination(destination.getName());
        airportPK.setName(name);
        Airport airport;
        
        airport = iaf.find(airportPK);
        if(airport == null){
            return true;
        }
        return false;
    }

    @Override
    public List<Airport> getAllAirports() {
        return iaf.findAll();
    }

    @Override
    public List<Airport> getAirportsByDestination(Destination destination) {
        return iaf.findByDestination(destination);
    }
    
    @Override
    public Airport getAirport(AirportPK airportPK){
        return iaf.find(airportPK);
    }
}
